<?php

include("config.php");
$_SESSION['logat'] = 1;
define('ANTIHACK', true);

if (file_exists("funct.php"))
	include_once "funct.php";
else
	die("Lipsesc fisiere.");

global $lang;

if (!isset($_SESSION["user"]))
	iesire();
else
	$user = user(clean($_SESSION["user"]));
if ($user === false || $user["logat"] == 0)
	iesire();
//in variabila $user vor fi stocate toate datele despre un utilizator
if (!isset($_GET["display"]))
	$_GET["display"] = "battles";
else
	clean($_GET["display"]);

switch ($_GET["display"]) { //titlul paginii + meniul activ. de fiecare data cand adaugi un caz la display trebuie sa-l pui si aici, altfel te va deloga
	case "battles": {
			$title = $lang["battles"];
			$meniu = 1;
			break;
		}
	case "judge_em": {
			$title = $lang["judge_em"];
			$meniu = 2;
			break;
		}
	case "how_to": {
			$title = $lang["how_to_roll"];
			$meniu = 3;
			break;
		}
	case "gangstaz": {
			$title = $lang["gangstaz"];
			$meniu = 4;
			break;
		}
	case "anunt": {
			$title = $lang['anunt'];
			$meniu = 5;
			break;
		}
	case "create": {
			$title = $lang["create_battle"];
			$meniu = 1;
			break;
		}
	case "message": {
			$title = $lang["message"];
			$meniu = 0;
			break;
		}
	case "battle": {
			$title = $lang["Battle"];
			$meniu = 0;
			break;
		}
	case "s_battle": {
			$title = $lang["see_battle"];
			$meniu = 2;
			break;
		}
	default: {
			$title = $lang["coming_soon"];
			$meniu = 0;
			break;
		}
}
include_once "html.php";
/*
 * fisierul html.php contine partea de sus a paginii si partea de jos
 * e un fel de tema, pentru fiecare tema vom modifica doar acel fisier si cel css
 * tot ce e scris in acest fisier nu va fi modificat de la o tema la alta
 * 
 * am facut cum ai dorit si va fi un singur output, fisierul index va fi modificat
 * ti-am scris mai jos unde tu trebuie sa scri, daca doresti, poti sa faci fisiere separate si apoi sa le incluzi in logged.php la cazul corespunzator din switch, cum am facut eu cu html.php
 * majoritatea comentariilor sunt adresate catre mic programator
 */
$continut = ""; //in aceasta variabila vom stoca continutul div-ul central
switch ($_GET["display"]) {
	case "battles": {
			$continut.='<div class="titlu">' . $lang["battles_desc"] . '</div>
                                <div class="batalii">';
			ver_rooms();
			$rooms = rooms_u($user["id"]);
			if ($rooms == false)
				$continut.=$lang["n_rooms"];
			else { //afisez bataliile active ale unui jucator
				for ($i = 0; $i < count($rooms); $i++) {
					if ($rooms[$i]["user2"] == -1)
						$user2 = $user;
					else
						$user2 = user($rooms[$i]["user2"]);
					$continut.='<div class="batalie">
                                                     <div class="batalie_content">
                                                         <div class="batalie_text">
                                                             <span class="span"><span>' . $lang['Street'] . ': </span><span style="color: #a07874;">' . $rooms[$i]["name"] . ' </span></span>
                                                                                                             <span class="span"><span> ' . $lang["Player"] . ': </span><a href="logged.php?display=user&id=' . $user2["id"] . '">' . $user2["name"] . '</a><span><br />
                                                             <span class="span"><span>' . $lang["Message"] . ': </span><span  style="color: #a07874;"> ' . $rooms[$i]["message"] . '</span></span>
                                                         </div>                                                            
                                                     </div>
                                                     <div class="vote_cerc">
                                                         <a href="logged.php?display=battle&id=' . $rooms[$i]["id"] . '"></a>
                                                     </div>
                                                 </div>';
				}
			}
			$continut.='<br/><div class="linie"></div><br/>';
			$rooms = rooms($user["id"]);
			if ($rooms !== false) { //afisez ultime batalii create in ordine descrescatoare de timp
				for ($i = 0; $i < count($rooms); $i++) {
					$user2 = user($rooms[$i]["user"]);
					if ($user2 !== false) {
						$value = $lang["Battle"];
						if ($user2["id"] == $user["id"])
							$value = $lang["Delete"];
						$continut.='<div class="batalie">
                                         <div class="batalie_content">
                                             <div class="batalie_text">
                                                 <span class="span"><span>' . $lang['Street'] . ': </span><span style="color: #a07874;">' . $rooms[$i]["name"] . ' </span></span>
												 <span class="span"><span> ' . $lang["Player"] . ': </span><a href="logged.php?display=user&id=' . $user2["id"] . '">' . $user2["name"] . '</a><span><br />
                                                 <span class="span"><span>' . $lang["Message"] . ': </span><span  style="color: #a07874;"> ' . $rooms[$i]["message"] . '</span></span>
                                             </div>                                                            
                                         </div>
                                         <div class="vote_cerc">
                                             <a href="logged.php?display=battle&id=' . $rooms[$i]["id"] . '"></a>
                                         </div>
                                     </div>';
					}
				}
			}
			$continut.='</div>
                                    <div class="butoane_jos">
                                        <a href="logged.php?display=create"></a>';
			$config = config();
			if ($config["battless_played"] < $user["rooms"])
				$continut.='<div class="anunt_interior">
							' . $lang["anunt_interior_battles0"] . '<br/>
                         ' . $lang["anunt_interior_battles"] . ' ' . $config["battless_played"] . ' ' . $lang["anunt_interior_battles2"] . '
                     </div>';
			$continut.='</div>';
			break;
		}
	case "battle": {
			if (!isset($_GET["id"]))
				iesire();
			$room = room(clean($_GET["id"]));
			if ($room !== false) {
				if ($room["user"] == $user["id"] && $room["user2"] != -1 && $room["intrat"] == 1)
					mysql_query("update " . $db["rooms"] . " set intrat='2' where id='" . $room["id"] . "'");
				if ($room["user"] != $user["id"] && $room["user2"] == -1) {
					$config = config();
					if ($config !== false)
						$time = a_ceas($config["time_battle1"]);
					else
						$time = a_ceas($terminare_b);
					mysql_query("update " . $db["rooms"] . " set end='" . $time . "', start='" . ceas() . "', user2='" . $user["id"] . "', intrat='1' where id='" . $room["id"] . "'");
				}
				$room = room($room["id"]);
				if ($room["user"] != $user["id"] && $room["user2"] != -1 && $room["user2"] != $user["id"]) //ma asigur ca nu intra altcineva in afara de cei doi jucatori in batalie
					iesire();
				if ($room["user2"] == -1)
					$juc["name"] = "";
				elseif ($room["user"] != $user["id"])
					$juc = user($room["user"]);
				else
					$juc = user($room["user2"]);
				if ($juc === false)
					$juc["name"] = "";
				elseif ($juc["name"] != "")
					$juc["name"] = "<span style='color: #c6c1b5;'>vs </span>" . $juc["name"];
				$continut.='<div class="titlu" id="titlu" style="background: #81898e;text-align: center;">' . $user["name"] . ' <span>' . $juc["name"] . '</span></div>';
				$mess = mess($room["id"]);
				$continut.='<div class="continut_s" style="height:330px;" id="mess">' . $mess . '</div>';
				$continut.='<form action="javascript:adauga(\'message\');">
                                            <textarea id="message"  class="textarea" placeholder="' . $lang["replica"] . '..."></textarea></br>
                                            <input type="submit" class="send"  value="" />
                                            <a href="delete_b.php?id=' . $room["id"] . '"><input type="button" class="buton" style="position: relative; marin:0 auto;" value="' . $lang["g_out"] . '" /></a>
                                    </form>';
				$rezultat = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "' order by date desc limit 0,1");
				$rand = mysql_fetch_array($rezultat);
				if (count($rand) > 1)
					$ceas = $rand["date"];
				else
					$ceas = ceas();
				$continut.='<script type="text/javascript">
								start_battle("' . $room["id"] . '","' . $ceas . '");
								$$("mess").scrollTop=$$("mess").scrollHeight;
							</script>';
			}
			else
				iesire();
			break;
		}
	case "create": {
			$continut.='<div class="titlu">' . $lang["create_desc"] . '</div>
                        <form action="create_battle.php" method="post">
                           <div class="continut_s">
                                <div class="create_left">
                                    Name: 
                                    Message:
                                </div> 
                                <div class="create_right" >
                                    <input type="text" name="name" class="input" style="border-radius: 0;width: 230px;background-color:#f4f1e9;" /><br/>
                                    <textarea name="message" class="input" style="resize: none;border-radius: 0;background-color:#f4f1e9;padding-top: 5px;width:230px; height:100px"></textarea><br/>
                                    <input type="submit" class="buton" value="' . $lang["send"] . '" />
                                </div>
                                <div class="over_black"></div>
                                <div class="over_black" style="top:134px;left: 172px; height: 107px;"></div>
                           </div>
                        </form>';
			break;
		}
	case "message": {
			if (!isset($_GET["message"]))
				iesire();
			$continut.='<div class="continut_s" style="height:auto;">' . clean($_GET["message"]) . '</div>';
			break;
		}
	case "judge_em": {
			ver_rooms2();
			$continut.='<div class="titlu">' . $lang["judge_em_d"] . '</div>';
			$continut.='<div class="continut_s" style="padding-top:15pt;">';
			$rooms = rooms_d(); //ultime camere terminate
			if ($rooms === false)
				$continut.=$lang["n_rooms_p"] . '<div class="linie"></div>';
			else {
				for ($i = 0; $i < count($rooms); $i++) {
					$user2 = user($rooms[$i]["user"]);
					$user3 = user($rooms[$i]["user2"]);
					$continut.='<div class="batalie">
                                                     <div class="batalie_content">
                                                         <div class="batalie_text">
                                                             <span class="span"><span>' . $lang['Street'] . ': </span><span style="color: #a07874;">' . $rooms[$i]["name"] . ' </span></span>
                                                                                                             <span class="span"><span> ' . $lang["Player"] . ': </span><a href="logged.php?display=user&id=' . $user2["id"] . '">' . $user2["name"] . '</a><span><br />
                                                             <span class="span"><span>' . $lang["Message"] . ': </span><span  style="color: #a07874;"> ' . $rooms[$i]["message"] . '</span></span>
                                                         </div>                                                            
                                                     </div>
                                                     <div class="vote_cerc">
                                                         <a href="logged.php?display=s_battle&id=' . $rooms[$i]["id"] . '" class="vote_href"></a>
                                                     </div>
                                                 </div>';
				}
			}
			$continut.='</div>';
			break;
		}
	case "s_battle": {
			if (!isset($_GET["id"]))
				iesire();
			$room = room(clean($_GET["id"]));
			global $rooms;
			if ($room["terminat"] != "1")
				iesire();
			if ($room !== false) {

				$user2 = user($room["user"]);
				$user3 = user($room["user2"]);
				if ($user2 === false) {
					$user2["name"] = "-";
					$user2["id"] = "-1";
				}
				if ($user3 === false) {
					$user3["name"] = "-";
					$user3["id"] = "-1";
				}
				$conv1 = conv_user($room["id"], $room["user"]);
				$conv2 = conv_user($room["id"], $room["user2"]);
				if ($conv1 === false || $conv2 === false) {
					delete_room($room["id"]);
					location("judge_em");
				}
				$sub1 = "";
				$sub2 = "";
				$continut.='<div class="batalie" style="margin: 0 auto;">
                                                 <div class="batalie_content">
                                                     <div class="batalie_text">
                                                         <span class="span"><span>' . $lang['Street'] . ': </span><span style="color: #a07874;">' . $room["name"] . ' </span></span><br />                                                                                                        
                                                         <span class="span"><span>' . $lang["Message"] . ': </span><span  style="color: #a07874;"> ' . $room["message"] . '</span></span>
                                                     </div>                                                            
                                                 </div>                                                 
                                             </div>';
				for ($i = 0; $i < count($conv1); $i++) {
					if ($i)
						$sub1.='<div class="i_s_battle" id="f_' . $i . '" style="display: none;">' . $conv1[$i] . '</div>';
					else
						$sub1.='<div class="i_s_battle"  id="f_' . $i . '">' . $conv1[$i] . '</div>';
				}
				for ($i = 0; $i < count($conv2); $i++) {
					if ($i)
						$sub2.='<div class="i_s_battle" id="l_' . $i . '" style="display: none;">' . $conv2[$i] . '</div>';
					else
						$sub2.='<div class="i_s_battle" id="l_' . $i . '">' . $conv2[$i] . '</div>';
				}
				$continut.='<script type="text/javascript">max1=' . count($conv1) . '-1; max2=' . count($conv2) . '-1;</script>
							<div class="continut_s" style="background-color: transparent; height: 300px; border:0px; padding-top: 20px;">
								<div class="s_battle_i" style="padding: 30px;">
								<img src="imagini/boxl.png" />
									<a style="margin-left: 37px; margin-top: -40px;float: left;" href="logged.php?display=user&id=' . $user3["id"] . '">' . $user2["name"] . '</a>' . $sub1 . '<div class="move"><a href="javascript:left(\'f_\')"><</a> <span id="f_">1</span> of ' . count($conv1) . ' <a href="javascript:right(\'f_\')">></a></div></div>
									<span style="display: inline-block; margin: 100px auto;">vs</span>
								<div class="s_battle_i" style="float: right;padding: 30px;">
								<img src="imagini/boxr.png" />
								<a style="margin-left: 37px;margin-top: -40px;float: left;" href="logged.php?display=user&id=' . $user3["id"] . '">' . $user3["name"] . '</a>' . $sub2 . '<div class="move"><a href="javascript:left(\'l_\')"><</a> <span id="l_">1</span> of ' . count($conv2) . ' <a href="javascript:right(\'l_\')">></a></div></div>
							</div>';
				if (ok_vote($room["id"], $user["id"]))
					$continut.='<div class="juc_"><a href="vote.php?id=' . $room["id"] . '&r=1">+</a> &nbsp&nbsp<a href="logged.php?display=user&id=' . $user3["id"] . '">' . $user2["name"] . '</a> vs <a href="logged.php?display=user&id=' . $user3["id"] . '">' . $user3["name"] . '</a>&nbsp&nbsp <a href="vote.php?id=' . $room["id"] . '&r=2">+</a></div>';
				else {
					$total = $room["votes"] + $room["votes2"];
					if ($total > 0) {
						$procent1 = $room["votes"] / $total * 100;
						$procent2 = $room["votes2"] / $total * 100;
					} else {
						$procent1 = 0;
						$procent2 = 0;
					}
					$procent1 = round($procent1, 1);
					$procent2 = round($procent2, 2);
					$continut.='<div class="juc_">' . $procent1 . "% &nbsp&nbsp&nbsp&nbsp " . $procent2 . '%</div>';
				}
			}
			else
				iesire();
			break;
		}
	case "how_to": {
			$continut.='<div class="continut_s">' . $lang["how_to_content"] . '</div>';
			break;
		}
	case "gangstaz": {
			if (!isset($_GET["page"]))
				$_GET["page"] = 0;
			else
				clean($_GET["page"]);
			if (!is_numeric($_GET["page"]))
				iesire();
			if ($_GET["page"] < 0)
				$_GET["page"] = 0;
			$_GET["page"] = (int) ($_GET["page"]);
			$jucatori = juc_clasament($_GET["page"]);
			if ($jucatori == false)
				$_GET["page"] = 0;
			$jucatori = juc_clasament($_GET["page"]);
			if ($jucatori !== false) {
				$continut.='<table class="clasament">
									<tr>
										<td style="width:10%;">' . $lang["rang"] . '</td>
										<td style="width:60%;">' . $lang["name"] . '</td>
										<td>' . $lang["points"] . '</td>
									</tr>';
				$rang = $_GET["page"] * 20 + 1;
				for ($i = 0; $i < count($jucatori); $i++) {
					if ($i == 0) {
						$continut.='<tr><td>' . ($rang + $i) . '</td><td><a href="logged.php?display=profile&id=' . $jucatori[$i]["id"] . '&r=' . ($rang + $i) . '"><img src="imagini/gold.png" alt="" width="20px" />' . $jucatori[$i]["name"] . '</a></td><td>' . $jucatori[$i]["puncte"] . '</td></tr>';
					}
					if ($i == 1) {
						$continut.='<tr><td>' . ($rang + $i) . '</td><td><a href="logged.php?display=profile&id=' . $jucatori[$i]["id"] . '&r=' . ($rang + $i) . '"><img src="imagini/silver.png" alt="" width="20px" />' . $jucatori[$i]["name"] . '</a></td><td>' . $jucatori[$i]["puncte"] . '</td></tr>';
					}
					if ($i == 2) {
						$continut.='<tr><td>' . ($rang + $i) . '</td><td><a href="logged.php?display=profile&id=' . $jucatori[$i]["id"] . '&r=' . ($rang + $i) . '"><img src="imagini/bronze.png" alt="" width="20px" />' . $jucatori[$i]["name"] . '</a></td><td>' . $jucatori[$i]["puncte"] . '</td></tr>';
					}
					if ($i > 2) {
						$continut.='<tr><td>' . ($rang + $i) . '</td><td><a href="logged.php?display=profile&id=' . $jucatori[$i]["id"] . '&r=' . ($rang + $i) . '">' . $jucatori[$i]["name"] . '</a></td><td>' . $jucatori[$i]["puncte"] . '</td></tr>';
					}
				}
				$continut.='</table>
								<div style="position: relative; margin-top: 10pt; width: 100%; margin-left: auto; margin-right: auto;">
									<span style="float: left;"><a href="logged.php?display=gangstaz&page=' . ($_GET["page"] - 1) . '"><img src="imagini/sageata_stanga.png" alt="" width="40px" /></a></span>
									<span style="float: right;"><a href="logged.php?display=gangstaz&page=' . ($_GET["page"] + 1) . '"><img src="imagini/sageata_dreapta.png" alt="" width="40px" /></a></span>
								</div>';
			}
			break;
		}
	case "profile": {
			if (!isset($_GET["id"])) {
				$continut.="<div class='continut_s'>
					<table class='tabel' style='width: 80%;'>
						<tr>
							<td style='text-align:left;'>{$lang["name"]} :</td>
							<td style='text-align:center;'>{$user["name"]} </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["email"]} :</td>
							<td style='text-align:center;'>{$user["email"]} </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["birtday"]} :</td>
							<td style='text-align:center;'>{$user["birday"]} </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["last_visit"]} :</td>
							<td style='text-align:center;'>{$user["last_visit"]} </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["rang"]} :</td>
							<td style='text-align:center;'>{$user["rang"]} </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["points"]} :</td>
							<td style='text-align:center;'>{$user["puncte"]} </td>
						</tr>
					</table>
					</div>
				";
			} else {
				if (!isset($_GET["r"]) || !is_numeric($_GET["r"]))
					iesire();
				clean($_GET["r"]);
				$user2 = user(clean($_GET["id"]));
				$continut.="
						<div class='continut_s'>
					<table class='tabel' style='width: 80%;'>
						<tr>
							<td style='text-align:left;'>{$lang["name"]} :</td>
							<td style='text-align:center;'>{$user2["name"]} </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["rang"]} :</td>
							<td style='text-align:center;'>{$_GET["r"]} </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["points"]} :</td>
							<td style='text-align:center;'>{$user2["puncte"]} </td>
						</tr>
					</table>
					</div>
					";
			}
			break;
		}
	case "settings": {
			$continut.="
					<div class='continut_s'>
					<form action='settings.php' method='post'>
					<table class='tabel' style='width: 90%;'>
						<tr>
							<td style='text-align:left;'>{$lang["n_password"]} :</td>
							<td style='text-align:center;'><input type='password' name='n_password' class='input' placeholder='{$lang["password"]}...' /> </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["n_mail"]} :</td>
							<td style='text-align:center;'><input type='email' name='n_mail' class='input' placeholder='{$lang["email"]}...' /> </td>
						</tr>
						<tr>
							<td style='text-align:left;'>{$lang["password"]} :</td>
							<td style='text-align:center;'><input type='password' name='password' class='input' placeholder='{$lang["password"]}...' /> </td>
						</tr>
						<tr>
							<td colspan='2'><input type='submit' class='buton' value='{$lang["send"]}'/></td>
						</tr>
					</table>
					</form>
					</div>
				";
			break;
		}
	case "contact": {
			$continut .='<div class="continut_s">
                        <div class="div_add">
                            <form action="" method="post" >';
			$error = '';
			if (isset($_POST['submit'])) {
				if (empty($_POST['nume']) || empty($_POST['email']) || empty($_POST['subiect']) || empty($_POST['mesaj'])) {
					$error .= 'All fields are required.<br />';
				}
				if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
					$error .= 'E-mail is incorrect.<br />';
				}
				if ($error != '') {
					$to = 'igor.mardari96@gmail.com';
					$nume = $_POST['nume'];
					$from = "From: " . $_POST['email'] . "";
					$subiect = $_POST['subiect'];
					$mesaj = $_POST['mesaj'];
					$body = 'E-mail de pe DissBattle, trimis de: ' . $nume . "\n Adresa lui/ei de e-mail: " . $_POST['email'] . "\n\n" . 'Mesaj: ' . $mesaj;
					if (@mail($to, $subiect, $body, $from)) {
						$error .= 'Message has succsesfully sent<br />';
					} else {
						$error .= 'Error with server<br />';
					}
				}
				$continut .= "<div style='padding: 10px;background: #DDD8CC;'>" . $error . "</div><br />";   # trebuie sa stergi punct si virgula,
			}
			isset($_POST['nume']) ? $nume = htmlentities($_POST['nume']) : $nume = '';
			isset($_POST['email']) ? $email = htmlentities($_POST['email']) : $email = '';
			isset($_POST['subiect']) ? $subiect = htmlentities($_POST['subiect']) : $subiect = '';
			isset($_POST['mesaj']) ? $mesaj = htmlentities($_POST['mesaj']) : $mesaj = '';
			$continut .= '
                                <label>Name : </label>            <input class="intut" type="text" name="nume" id="nume" size="32" color="white" maxlength="40" value="' . $nume . '" /><br /><br />
                                <label>E-mail : </label>          <input class="intut" type="text" name="email" id="email" size="32" maxlength="58"  value="' . $email . '" /><br /><br />
                                <label>Subject : </label>         <input class="intut" type="text" name="subiect" id="subiect" size="32" maxlength="70"  value="' . $subiect . '" /><br /><br />
                                <label>Message : </label>         <textarea  class="intut" name="mesaj" id="mesaj" cols="26" rows="6"  >' . $mesaj . '</textarea><br /> <br /> <br /> <br /> <br /> <br /> 
                                <input type="submit" class="button" name="submit" style=""  /> 
                            </form>
                        </div></div>';
			break;
		}
	default : {
			$continut.='<div class="continut_s" style="padding-top:30pt;">' . $lang["coming_soon"] . '</div>';
			break;
		}
}

echo $header . '' . $continut . '' . $footer;
?>
